<?php
/*
 * @Description:高危许可证管理
 * @FilePath: \auxface_api\app\certificate\controller\user\v1\Risk.php
 */

declare(strict_types=1);

namespace app\certificate\controller\user\v1;


use think\facade\Db;
use think\Request;

class Area
{
    /**
     * @Description: 获取区域列表
     * @param {Request} $request
     */
    public function getListByTypeUuid(Request $request)
    {
        $data  = $request->data;
        $searchWhere = [
            ['rt.uuid', '=', $data['typeUuid']],
            ['ta.wxappUserUuid', '=', $request->uuid],
            ['at.type', '=', $data['type']],
            ['aw.isDelete', '=', 0],
            ['ta.isDelete', '=', 0],
        ];
        $type = $data['type'] ?? 1;
        if ($type == 1) {
            $list = DB::table('certificate_riskType')
                ->alias('rt')
                ->join('certificate_areaType at', 'rt.uuid = at.typeUuid')
                ->join('certificate_area a', 'a.uuid=at.areaUuid')
                ->join('certificate_areaWorker aw', 'aw.areaUuid = a.uuid')
                ->join('certificate_riskTypeAuth ta', 'ta.wxappUserUuid = aw.wxappUserUuid')
                ->order(['rt.id desc'])
                ->where($searchWhere)
                ->field(['a.name', 'a.uuid'])
                ->distinct(true)
                ->select()->toArray();
        }else if($type ==2) {
            $list = DB::table('certificate_heavyMachineCheckType')
                ->alias('rt')
                ->join('certificate_areaType at', 'rt.uuid = at.typeUuid')
                ->join('certificate_area a', 'a.uuid=at.areaUuid')
                ->join('certificate_areaWorker aw', 'aw.areaUuid = a.uuid')
                ->join('certificate_heavyMachineCheckTypeAuth ta', 'ta.wxappUserUuid = aw.wxappUserUuid')
                ->order(['rt.id desc'])
                ->where($searchWhere)
                ->where('ta.isDay', 1)
                ->field(['a.name', 'a.uuid'])
                ->distinct(true)
                ->select()->toArray();
        } else if($type ==3) {
            $list = DB::table('certificate_machineRiskType')
                ->alias('rt')
                ->join('certificate_areaType at', 'rt.uuid = at.typeUuid')
                ->join('certificate_area a', 'a.uuid=at.areaUuid')
                ->join('certificate_areaWorker aw', 'aw.areaUuid = a.uuid')
                ->join('certificate_flowFormWorker ta', 'ta.wxappUserUuid = aw.wxappUserUuid')
                ->order(['rt.id desc'])
                ->where($searchWhere)
                ->field(['a.name', 'a.uuid'])
                ->distinct(true)
                ->select()->toArray();
        }
        return result(SUCCESS, $list, '');
    }
}
